#!/usr/bin/env python

# --------------------------------------
# projects/python/TestPrimes.py
# Copyright (C) 2009
# Eric Shrewsberry and Daniel Treiber
# --------------------------------------

import main
import unittest

# ----------
# TestReader
# ----------

class TestReader (object) :
    def __init__ (self, s) :
        self.s = s

    def read (self) :
        return self.s

# ----------
# TestWriter
# ----------

class TestWriter (object) :
    def str (self) :
        return self.s

    def write (self, *a) :
        self.s = str(a[0])
        for w in a[1:] :
            self.s += ' '
            self.s += str(w)
        self.s += '\n'

# -----------
# TestCollatz
# -----------

class TestCollatz (unittest.TestCase) :
    """
    Defines unit tests for the primes project.
    """
    # ----
    # read
    # ----

    def test_read (self) :
        reader = TestReader('9\n')
        main.my_read(reader)
        self.assert_(main.p ==  9)

    # ----
    # eval
    # ----

    def test_eval1 (self) :
        main.p =  123123
        main.my_eval()
        self.assert_(main.primes == [123113, 3, 2, 5])
	
    def test_eval2 (self) :
        main.p =  456456
        main.my_eval()
        self.assert_(main.primes == [456439, 13, 2, 2])
	
    def test_eval3 (self) :
        main.p =  159
        main.my_eval()
        self.assert_(main.primes == [151, 2, 3, 3])

    def test_eval4 (self) :
        main.p =  7854
        main.my_eval()
        self.assert_(main.primes == [7841, 5, 5, 3])
	
    def test_eval5 (self) :
        main.p =  25987
        main.my_eval()
        self.assert_(main.primes == [25981, 2, 2, 2])
	
    def test_eval6 (self) :
        main.p =  5126547
        main.my_eval()
        self.assert_(main.primes == [5126497, 43, 5, 2])
	
    def test_eval7 (self) :
        main.p =  84512
        main.my_eval()
        self.assert_(main.primes == [84503, 3, 3, 3])
	
    def test_eval8 (self) :
        main.p =  654778
        main.my_eval()
        self.assert_(main.primes == [654767, 5, 3, 3])
	
    def test_eval9 (self) :
        main.p =  4512687
        main.my_eval()
        self.assert_(main.primes == [4512653, 29, 3, 2])

    def test_eval10 (self) :
        main.p =  465123
        main.my_eval()
        self.assert_(main.primes == [465107, 11, 3, 2])
	
    def test_eval11 (self) :
        main.p =  456789
        main.my_eval()
        self.assert_(main.primes == [456769, 13, 5, 2])
	
    def test_eval12 (self) :
        main.p =  159745
        main.my_eval()
        self.assert_(main.primes == [159739, 2, 2, 2])
	
    def test_eval13 (self) :
        main.p =  156584
        main.my_eval()
        self.assert_(main.primes == [156577, 2, 2, 3])
	
    def test_eval14 (self) :
        main.p =  78958
        main.my_eval()
        self.assert_(main.primes == [78941, 13, 2, 2])
	
    def test_eval15 (self) :
        main.p =  658748
        main.my_eval()
        self.assert_(main.primes == [658703, 41, 2, 2])

    def test_eval16 (self) :
        main.p =  1258756
        main.my_eval()
        self.assert_(main.primes == [1258723, 29, 2, 2])
	
    def test_eval17 (self) :
        main.p =  15987
        main.my_eval()
        self.assert_(main.primes == [15973, 7, 5, 2])
	
    def test_eval18 (self) :
        main.p =  2589
        main.my_eval()
        self.assert_(main.primes == [2579, 3, 2, 5])
	
    def test_eval19 (self) :
        main.p =  1598756
        main.my_eval()
        self.assert_(main.primes == [1598743, 5, 5, 3])
	
    def test_eval20 (self) :
        main.p =  125487
        main.my_eval()
        self.assert_(main.primes == [125471, 11, 3, 2])
	
    def test_eval21 (self) :
        main.p =  36985
        main.my_eval()
        self.assert_(main.primes == [36979, 2, 2, 2])

    def test_eval22 (self) :
        main.p =  1589
        main.my_eval()
        self.assert_(main.primes == [1583, 2, 2, 2])
	
    def test_eval23 (self) :
        main.p =  987564
        main.my_eval()
        self.assert_(main.primes == [987541, 19, 2, 2])
	
    def test_eval24 (self) :
        main.p =  125
        main.my_eval()
        self.assert_(main.primes == [113, 5, 5, 2])

    # -----
    # print
    # -----

    def test_print (self) :
        main.primes = [1, 2, 3, 4]
        writer = TestWriter()
        main.my_print(writer)
        self.assert_(writer.str() == '1 2 3 4\n')
	
    def test_print1 (self) :
        main.primes = [1, 2, 3, -1]
        writer = TestWriter()
        main.my_print(writer)
        self.assert_(writer.str() == 'Impossible.\n')

if __name__ == "__main__" :
    unittest.main()

